package com.lcb.jmm.volatiledome;

/****
 * @Author:lcb
 * @Description:死锁
 * @Date: 2020-12-23 11:31
 *****/
public class Deadlock {
    private final static String resource_a = "A";
    private final static String resource_b = "B";

    public static void deadLock() {
        Thread threadA = new Thread(new Runnable() {
            public void run() {
                synchronized (resource_a) {
                    System.out.println("获取A资源");
                    try {
                        Thread.sleep(2000);
                        synchronized (resource_b) {
                            System.out.println("获取B资源");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            public void run() {
                synchronized (resource_b) {
                    System.out.println("获取B资源");
                    synchronized (resource_a) {
                        System.out.println("获取A资源");
                    }
                }
            }
        });

        threadA.start();
        threadB.start();
    }

    public static void main(String[] args) {
        deadLock();
        System.out.println("运行结束!");
    }
}
